package com.nanhai.buddhist.college.module.study.controller.admin.studyplan;

import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;
import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import com.nanhai.buddhist.college.module.collegebase.api.college.CollegeApi;
import com.nanhai.buddhist.college.module.collegebase.api.college.dto.CollegesRespDTO;
import com.nanhai.buddhist.college.module.study.controller.admin.studyplan.vo.*;
import com.nanhai.buddhist.college.module.study.convert.studyplan.StudyPlanConvert;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studyplan.StudyPlanDO;
import com.nanhai.buddhist.college.module.study.service.studyplan.StudyPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;
import static com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils.convertList;
import static com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils.convertMap;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 教学计划")
@RestController
@RequestMapping("/study/plan")
@Validated
public class StudyPlanController {

    @Resource
    private StudyPlanService planService;

    @PostMapping("/create")
    @ApiOperation("创建教学计划")
    //@PreAuthorize("@ss.hasPermission('study:plan:create')")
    public CommonResult<String> createPlan(@Valid @RequestBody StudyPlanCreateReqVO createReqVO) {
        return success(planService.createPlan(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新教学计划")
   // @PreAuthorize("@ss.hasPermission('study:plan:update')")
    public CommonResult<Boolean> updatePlan(@Valid @RequestBody StudyPlanUpdateReqVO updateReqVO) {
        planService.updatePlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除教学计划")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
   // @PreAuthorize("@ss.hasPermission('study:plan:delete')")
    public CommonResult<Boolean> deletePlan(@RequestParam("id") String id) {
        planService.deletePlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得教学计划")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    //@PreAuthorize("@ss.hasPermission('study:plan:query')")
    public CommonResult<StudyPlanRespVO> getPlan(@RequestParam("id") String id) {
        StudyPlanDO plan = planService.getPlan(id);
        return success(StudyPlanConvert.INSTANCE.convert(plan));
    }

    @GetMapping("/list")
    @ApiOperation("获得教学计划列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
   // @PreAuthorize("@ss.hasPermission('study:plan:query')")
    public CommonResult<List<StudyPlanRespVO>> getPlanList(@RequestParam("ids") Collection<String> ids) {
        List<StudyPlanDO> list = planService.getPlanList(ids);
        return success(StudyPlanConvert.INSTANCE.convertList(list));
    }


    @Resource
    private CollegeApi collegeApi;

    @GetMapping("/page")
    @ApiOperation("获得教学计划分页")
   //@PreAuthorize("@ss.hasPermission('study:plan:query')")
    public CommonResult<PageResult<StudyPlanRespVO>> getPlanPage(@Valid StudyPlanPageReqVO pageVO) {
        PageResult<StudyPlanDO> pageResult = planService.getPlanPage(pageVO);

        List<String> collect = convertList(pageResult.getList(),StudyPlanDO::getCollegesId);

        // 组装学院信息
        CommonResult<List<CollegesRespDTO>> collegesList = collegeApi.getCollegesList(collect);
        Map<String, CollegesRespDTO> stringCollegesRespDTOMap = convertMap(collegesList.getData(), CollegesRespDTO::getId);
        PageResult<StudyPlanRespVO> studyPlanRespVOPageResult = StudyPlanConvert.INSTANCE.convertPage(pageResult);
        studyPlanRespVOPageResult.getList().forEach(item->{
            item.setCollegeName(Optional.ofNullable(stringCollegesRespDTOMap).map(s-> s.get(item.getCollegesId())).map(CollegesRespDTO::getCollegeName).orElse(""));
        });
        return success(studyPlanRespVOPageResult);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出教学计划 Excel")
    //@PreAuthorize("@ss.hasPermission('study:plan:export')")
    @OperateLog(type = EXPORT)
    public void exportPlanExcel(@Valid StudyPlanExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<StudyPlanDO> list = planService.getPlanList(exportReqVO);
        // 导出 Excel
        List<StudyPlanExcelVO> datas = StudyPlanConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "教学计划.xls", "数据", StudyPlanExcelVO.class, datas);
    }

}
